
clear all
global main "/Users/ingahei/UiO Dropbox/IngaB Heiland/Ais project/Repository"
cd "$main"

global data "$main/data_intermediate"
global input "$main/data_input"
global figures "$main/figures"

global ps "/Users/ingahei/UiO Dropbox/IngaB Heiland/Documents/Projekte/Risk channel/Data/PSchott"


***** distance to Panama

u "$input/gravdataFTA1319monthly.dta", clear
keep if iso_o=="PAN"
keep iso* distw
g lndPA = ln(distw)
keep iso* lndPA
duplicates drop
save "$data/distPA", replace

**** container shares at HS-2-digit level

u "$ps/imp_detl_yearly_119n", clear /* raw data: download from http://faculty.som.yale.edu/peterschott/sub_international.htm and place in $source folder*/
g d2=floor(commo/100000000)
collapse (sum) *val_yr , by(year d2) /*collapse over products and customs clearance location*/
g cnt_share=cnt_val/gen_val
keep d2 cnt_share
save "$data/cnt_share_2d_2019", replace


u "$ps/imp_detl_yearly_113n", clear /* raw data: download from http://faculty.som.yale.edu/peterschott/sub_international.htm and place in $source folder*/
g d2=floor(commo/100000000)
collapse (sum) *val_yr , by(year d2) /*collapse over products and customs clearance location*/
g cnt_share=cnt_val/gen_val
keep d2 cnt_share
save "$data/cnt_share_2d_2013", replace





**** country pairs with direct links


u "$data/ship_port_to_port_wclusters", clear
keep *country
duplicates drop
rename D_country iso2
merge m:1 iso2 using "$input/iso2toiso3_uqe", keepusing(iso3)
drop if _merge==2
drop _merge
replace iso3="TWN" if iso2=="TW"
replace iso3="PRI" if iso2=="PR"
rename iso3 iso_i
drop iso2

rename A_c iso2
merge m:1 iso2 using "$input/iso2toiso3_uqe", keepusing(iso3)
drop if _merge==2
drop _merge

replace iso3="TWN" if iso2=="TW"
replace iso3="PRI" if iso2=="PR"
rename iso3 iso_j
drop iso2
save "$data/direct_links", replace

** summary statistics on direct links
u "$data/direct_links", clear
merge m:1 iso_i iso_j using "$data/PESPbyHr_bil514" // PanExp1
keep if _m==3
count if XP>0
di r(N)/_N





xxxx to be continued


*** regressions at the HS 2-digit levelsof


*** total bilateral trade, quarterly
u "$data/allHS2_2d", clear

rename reporteriso iso_j
rename partneriso iso_i
egen CO = total(cifvalue_co), by(period iso_j)
egen VA = total(cifvalue), by(period iso_j)
replace cifvalue=. if VA==0  // replace zeros with missing if reporter did not report anything in this period
*replace cifvalue_nr=. if VA==0
replace cifvalue_co=. if CO==0
*replace cifvalue_co_nr=. if CO==0

rename cifvalue* value*
tostring period, replace
g year = substr(perio,1,4)
g month = substr(perio,5,2)
destring year month, replace
g q=ceil(month/3)


gcollapse (sum) val* (count) nmvalue = value nmvalue_co = value_co, by(year q iso_i iso_j d2) fast // collapse from monthly to quarterly
replace value=. if nmvalue!=3.         // keep only quarters where all three months are reported
replace value_co=. if nmvalue_co!=3
*replace value_nr=. if nmvalue_nr!=3
replace value_co=value if value_co==.  // replace missing imports by country of consignment with imports by country of origin if available
drop nmvalu*

g iso_o = iso_i 
g iso_d = iso_j
replace iso_o="ROM" if iso_o=="ROU"
replace iso_d="ROM" if iso_d=="ROU"
replace iso_o="ZAR" if iso_o=="COD"
replace iso_d="ZAR" if iso_d=="COD"
replace iso_o="TMP" if iso_o=="TLS"
replace iso_d="TMP" if iso_d=="TLS"
replace iso_o="YUG" if iso_o=="SRB"
replace iso_d="YUG" if iso_d=="SRB"
replace iso_o="YUG" if iso_o=="MNE"
replace iso_d="YUG" if iso_d=="MNE"
replace iso_o="PAL" if iso_o=="PSE"
replace iso_d="PAL" if iso_d=="PSE"


save "$data/allHS_2d_q", replace


use "$data/allHS_2d_q", clear

merge m:1 iso_o iso_d q year using "$input/gravdataFTA1319quarterly.dta" //1:m because Serbia and MNE are one country in the gravity data
keep if _m==3
drop _m 

sort year q
egen perid=group(year q)
egen cid_i=group(iso_i)
egen cid_j=group(iso_j)

merge m:1 iso_i iso_j using "$data/PESPbyHr_bil514" // PanExp1
keep if _merge==3
drop _merge 
rename XP PanExp1

g post=0
replace post=1 if (q>2 & year==2016) | year>2016

g postPanExp1=post*PanExp1
g postxlnd=post*lnd
g postxcontig=post*contig
g postxcomlang=post*comlang_o
g postNEOP_pre=post*steuNEOP_pre

foreach var of varlist value* {
	g ln`var'=ln(`var')
}

merge m:1 d2 using "$data/cnt_share_2d_2019"
drop if _m==2
drop _m
g postPanExp1_cont=postPanExp1*cnt_share

g preval=value_co if post==0
egen mpreval=mean(preval), by(cid_i cid_j d2)

sum cnt_share, d


capture drop bin*



g binF1=0
replace binF1 = postPanExp1 if cnt_share<.25
g binF2=0
replace binF2 = postPanExp1 if cnt_share>=.25 & cnt_sh<.5
g binF3=0
replace binF3 = postPanExp1 if cnt_share>=.5 & cnt_sh<.75
g binF4=0
replace binF4 = postPanExp1 if cnt_share>=.75 


eststo inter: reghdfe lnvalue_co  postPanExp1_cont, ab(cid_i#perid#d2 cid_j#perid#d2 cid_j#cid_i#d2  cid_j#cid_i#perid) cluster(cid_i cid_j d2)

replace binF1=0
eststo b3: reghdfe lnvalue_co binF1 binF2 binF3 binF4 if cnt_share!=., ab(cid_i#perid#d2 cid_j#perid#d2 cid_j#cid_i#d2  cid_j#cid_i#perid) cluster(cid_i cid_j d2)
global coeflab "1 "(0, .25)" 2 "(.25, .5)" 3 "(.5, .75)" 4 "(.75, 1)"" 
coefplot, drop(_cons) scale(1) vert  xlabel($coeflab) graphregion(color(white)) ci(90) omitted ytitle("coefficient estimate") xtitle("container share")
graph export "$figures/coefplot_cnt_bins.png", replace


esttab inter b3, b(3) ar2 se star( * .1 ** .05 *** .01)



